<?php
/**
 * Description of MyEmail
 *
 * @author milan.frydrych
 */
class MyEmail {
    private static $instances = 0;
    
    private $email;
    private $subject;
    private $message;
    
    public function __construct($email, $subject, $message) {
        if(MyEmail::$instances==0){
            MyEmail::$instances==1;
            $this->email = $email;
            $this->subject = $subject;
            $this->message = $message;
        }
    }
    
    public function __destruct() {
         MyEmail::$instances==0;
    }
    
    public function Send(){     
        $headers = "MIME-Version: 1.0"
                    . PHP_EOL . "Content-Type: text/plain; charset=\"utf-8\""
                    . PHP_EOL . "Content-Transfer-Encoding: 7bit"
                    . PHP_EOL . "From: admin@frydrych.php5.cz"
                    . ($headers ? PHP_EOL . $headers : "");
        iconv_set_encoding("internal_encoding", "utf-8");
        $subject = iconv_mime_encode("Subject", substr($subject = "Subject: ".$this->subject, strLen("Subject: ")));
        try {
            mail('milan.frydrych@gmail.com', $subject, $this->message, $headers);
        }
        catch (Exception $e){
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
    }
    
    public function ValidateForm($tName, $tEmail){
        $error='';
        
        if (strpbrk($tName, "0123456789-+*/")) $error.= "Jméno obsahuje nepovolené znaky. ";
        $regexEmail = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
        if (!preg_match($regexEmail, $tEmail)) $error.="Email nemá správný formát. ";
        
        return $error;
    }
}

?>
